<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        canvas{
            border:1px solid;
        }

    </style>
</head>
<body>
    <img src="" />
    <canvas width="500" height="500" id="mycanvas"></canvas>
    <script>
        // 1.获取图片把图片绘制canvas画布上 ；
        // 2.通过canvas 提供的toDataUrl把图片转换成base64格式
        var canvas = document.querySelector("#mycanvas");
        var ctx = canvas.getContext("2d");

        var imgObj = new Image();
        imgObj.src = "./1.png";
        // console.log(imgObj);
        // 等待图片加载完毕之后把图片绘制再canvas
        imgObj.onload = function(){
            console.log("图片加载完毕");
            ctx.drawImage(this,0,0,500,500);
           var res =  canvas.toDataURL();
           console.log(res);
        }

        var src = ""



    </script>
</body>
</html>